//
//  DYZCarSeriesController.m
//  DYZCars
//
//  Created by 杨瑞 on 16/8/20.
//  Copyright © 2016年 MF. All rights reserved.
//

#import "DYZCarSeriesController.h"
#import "DYZOwnCarCellCell.h"
#import "DYZCarSeries.h"
#import "DYZCarSeriesInfo.h"
#import "DSHTTPClient.h"
#import "DYZCarDetailInfoController.h"
#import "UIColor+Addition.h"

static NSString *cellid = @"cellid";

@interface DYZCarSeriesController ()
/**
 *  车系列数据
 */
@property (nonatomic, strong) NSArray *carSeriesArr;
@property (nonatomic, copy) NSString *carId;
@end

@implementation DYZCarSeriesController

- (void)viewDidLoad {
    NSLog(@"------%@",self.carBrandId);
    [super viewDidLoad];
    [self.tableView registerNib:[UINib nibWithNibName:@"DYZOwnCarCellCell" bundle:nil] forCellReuseIdentifier:cellid];
    self.carId = [self.carBrandId stringByReplacingOccurrencesOfString:@"DB1000" withString:@"BD100"];
    [self series];
    NSLog(@"%@",self.carId);
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return self.carSeriesArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    DYZCarSeries *carSeries = self.carSeriesArr[section];
    return carSeries.carlineList.count;
}

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    DYZOwnCarCellCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid forIndexPath:indexPath];
     DYZCarSeries *carSeries = self.carSeriesArr[indexPath.section];
     DYZCarSeriesInfo *carSeriesInfo = [DYZCarSeriesInfo yy_modelWithDictionary:carSeries.carlineList[indexPath.row]];
     cell.carCategoryLabel.text =[self.navigationItem.title stringByAppendingString: carSeriesInfo.carLineName];
 
 // Configure the cell...
 
 return cell;
 }
//更改cell组头
//改变组头的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
//改变cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 45;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor colorWithHex:0xf0f0f0];
    UILabel *textLabel = [[UILabel alloc]init];
    [textLabel setFont:[UIFont systemFontOfSize:12]];
    [textLabel sizeToFit];
    [headerView addSubview:textLabel];
    [textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.bottom.equalTo(headerView);
    }];
    //    if (section == 0) {
    //        textLabel.text = @"奔驰";
    //    }else if (section == 1){
    //        textLabel.text = @"宾利";
    //    }else{
    //        textLabel.text = @"宝马";
    //    }
    DYZCarSeries *carSeries = self.carSeriesArr[section];
    textLabel.text = carSeries.label;
    return headerView;
}

//获取数据
- (void)series{
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:@"1" forKey:@"accountid"];
    [param setValue:self.carId forKey:@"brandId"];
    
    [DSHTTPClient postUrlString:@"http://iosapi.itcast.cn/car/series.json.php" withParam:param withSuccessBlock:^(id data) {
        //        NSLog(@"%@",data[@"data"][@"carList"]);
//        NSLog(@"%@",data);
        NSArray *array = data[@"data"][@"carList"];
        NSLog(@"%@",array);
        NSMutableArray *carSeriesArray = [NSMutableArray array];
        for (NSDictionary *dict in array) {
            DYZCarSeries *carSeries = [DYZCarSeries yy_modelWithDictionary:dict];
            NSMutableArray *carSeriesInfoArray = [NSMutableArray array];
            for (NSDictionary *dict in carSeries.carlineList) {
                DYZCarSeriesInfo *carSeriesInfo = [DYZCarSeriesInfo yy_modelWithDictionary:dict];
                [carSeriesInfoArray addObject:carSeriesInfo];
            }
            [carSeriesArray addObject:carSeries];
            self.carSeriesArr = carSeriesArray;
            [self.tableView reloadData];
        }
    } withFailedBlock:^(NSError *error) {
        NSLog(@"%@",error);
    } withErrorBlock:^(NSString *message) {
        NSLog(@"%@",message);
    }];
}
#pragma mark - 懒加载
-(NSArray *)carSeriesArr{
    if (_carSeriesArr == nil) {
        _carSeriesArr = [NSArray array];
    }
    return _carSeriesArr;
}
#pragma mark - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    if ([self.navigationItem.title isEqualToString:@"宾利"]) {
    DYZCarSeries *carSeries = self.carSeriesArr[indexPath.section];
    DYZCarSeriesInfo *carSeriesInfo = [DYZCarSeriesInfo yy_modelWithDictionary:carSeries.carlineList[indexPath.row]];
    DYZCarDetailInfoController *carVC = [[DYZCarDetailInfoController alloc]init];
    carVC.carSeriesName =[self.navigationItem.title stringByAppendingString: carSeriesInfo.carLineName];
//    DYZCarModel *model = self.cars[indexPath.section];
//    carVC.navigationItem.title = model.car[indexPath.row];
    [self.navigationController pushViewController:carVC animated:YES];
    //    }
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
